package at.dl.android.dota.adviser;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils.TruncateAt;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class ShowItemActivity extends ActivityWithDB {

    public final static String ITEM_NAME = "ITEM_NAME";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_item);
        
        final Bundle extras = getIntent().getExtras();
        String name = extras.getString(ITEM_NAME);
        
        SQLiteDatabase db = getDataBaseHelper().getReadableDatabase();
        String qry = "SELECT * FROM items WHERE name = \"" + name + "\"";
        Cursor cursor = db.rawQuery(qry,new String[0]);
        cursor.moveToFirst();
        
        ImageView img = (ImageView) findViewById(R.id.img);
        img.setImageResource(cursor.getInt(cursor.getColumnIndex(ColumnNames.Items.item_image_id)));
        TextView t = (TextView) findViewById(R.id.name);
        t.setText(cursor.getString(cursor.getColumnIndex(ColumnNames.Items.name)));
        t = (TextView) findViewById(R.id.total_costs);
        int totalCosts = cursor.getInt(cursor.getColumnIndex(ColumnNames.Items.total_costs));
        t.setText("" + totalCosts);
        
        setBoni(cursor);
        
        setComponents(cursor);
        
        setSuperItems(name);
        
        setShops(cursor);
    }

    private void setBoni ( Cursor cursor ) {
        TableLayout tableLayout = (TableLayout) findViewById(R.id.boni);
        addIntIfHasValue(cursor,tableLayout,ColumnNames.Items.upgradeable,R.string.upgradeable,R.string.upgradeable_postfix);
        addStringIfHasValue(cursor,tableLayout,ColumnNames.Items.strength,R.string.strength);
        addStringIfHasValue(cursor,tableLayout,ColumnNames.Items.agility,R.string.agility);
        addStringIfHasValue(cursor,tableLayout,ColumnNames.Items.intelligence,R.string.intelligence);
        addStringIfHasValue(cursor,tableLayout,ColumnNames.Items.health,R.string.health);
        addStringIfHasValue(cursor,tableLayout,ColumnNames.Items.mana,R.string.mana);
        addStringIfHasValue(cursor,tableLayout,ColumnNames.Items.damage,R.string.damage);
        addStringIfHasValue(cursor,tableLayout,ColumnNames.Items.health_reg,R.string.health_reg);
        addStringIfHasValue(cursor,tableLayout,ColumnNames.Items.mana_reg,R.string.mana_reg);
        addStringIfHasValue(cursor,tableLayout,ColumnNames.Items.armor,R.string.armor);
        addStringIfHasValue(cursor,tableLayout,ColumnNames.Items.evasion,R.string.evasion);
        addStringIfHasValue(cursor,tableLayout,ColumnNames.Items.magic_resistance,R.string.magic_resistance);
        addStringIfHasValue(cursor,tableLayout,ColumnNames.Items.attack_speed,R.string.attack_speed);
        addStringIfHasValue(cursor,tableLayout,ColumnNames.Items.movement_speed,R.string.mov_speed);
        addBooleanIfHasValue(cursor,tableLayout,ColumnNames.Items.aura,R.string.aura);
        addStringIfHasValue(cursor,tableLayout,ColumnNames.Items.special,R.string.special);
        addBooleanIfHasValue(cursor,tableLayout,ColumnNames.Items.active_feature,R.string.active_feature);
        addBooleanIfHasValue(cursor,tableLayout,ColumnNames.Items.charges,R.string.charges);
        addBooleanIfHasValue(cursor,tableLayout,ColumnNames.Items.passive_feature,R.string.passive_feature);
        addStringIfHasValue(cursor,tableLayout,ColumnNames.Items.cooldown,R.string.cooldown,R.string.cooldown2);
        addStringIfHasValue(cursor,tableLayout,ColumnNames.Items.orb_effect,R.string.orb_effect);
    }

    private void setComponents ( Cursor cursor ) {
        int[] ids = {
                cursor.getInt(cursor.getColumnIndex(ColumnNames.Items.component1_image_id)),
                cursor.getInt(cursor.getColumnIndex(ColumnNames.Items.component2_image_id)),
                cursor.getInt(cursor.getColumnIndex(ColumnNames.Items.component3_image_id)),
                cursor.getInt(cursor.getColumnIndex(ColumnNames.Items.component4_image_id))
        };
        String[] names = {
                cursor.getString(cursor.getColumnIndex(ColumnNames.Items.component1)),
                cursor.getString(cursor.getColumnIndex(ColumnNames.Items.component2)),
                cursor.getString(cursor.getColumnIndex(ColumnNames.Items.component3)),
                cursor.getString(cursor.getColumnIndex(ColumnNames.Items.component4))
        };
        int[] costs = {
                cursor.getInt(cursor.getColumnIndex(ColumnNames.Items.component1_costs)),
                cursor.getInt(cursor.getColumnIndex(ColumnNames.Items.component2_costs)),
                cursor.getInt(cursor.getColumnIndex(ColumnNames.Items.component3_costs)),
                cursor.getInt(cursor.getColumnIndex(ColumnNames.Items.component4_costs))
        };
        int recipeCosts = cursor.getInt(cursor.getColumnIndex(ColumnNames.Items.recipe_costs));
        if ( recipeCosts > 0 ) {
            int[] ids2 = new int[ids.length + 1];
            System.arraycopy(ids,0,ids2,1,ids.length);
            ids = ids2;
            ids[0] = R.drawable.recipe;
            String[] names2 = new String[names.length + 1];
            System.arraycopy(names,0,names2,1,names.length);
            names = names2;
            names[0] = getString(R.string.recipe);
            int[] costs2 = new int[costs.length + 1];
            System.arraycopy(costs,0,costs2,1,costs.length);
            costs = costs2;
            costs[0] = recipeCosts;
        }
        Gallery componentGalleryView = (Gallery) findViewById(R.id.components_gallery);
        GalleryItemImageAdapter sia = new GalleryItemImageAdapter(componentGalleryView,this,ids,names,costs);
        componentGalleryView.setAdapter(sia);
        sia.adjustSelection(); // bloody damned gallery!
    }
    
    private void setSuperItems ( String name ) {
        String qry 
            = "SELECT " 
                + ColumnNames.Items.name + ", " 
                + ColumnNames.Items.item_image_id + ", "
                + ColumnNames.Items.total_costs
            + " FROM items" 
            + " WHERE component1 = \"" + name + "\""
            +    " OR component2 = \"" + name + "\""
            +    " OR component3 = \"" + name + "\""
            +    " OR component4 = \"" + name + "\"";
        SQLiteDatabase db = getDataBaseHelper().getReadableDatabase();
        Cursor cursor = db.rawQuery(qry,new String[0]);
        final int maxCnt = 20;
        int[] ids = new int[maxCnt];
        String[] names = new String[maxCnt];
        int[] costs = new int[maxCnt];
        
        boolean hasMoreResults = cursor.moveToFirst();
        for ( int i = 0; i < maxCnt && hasMoreResults; i++  ) {
            ids[i] = cursor.getInt(cursor.getColumnIndex(ColumnNames.Items.item_image_id));
            names[i] = cursor.getString(cursor.getColumnIndex(ColumnNames.Items.name));
            costs[i] = cursor.getInt(cursor.getColumnIndex(ColumnNames.Items.total_costs));
            hasMoreResults = cursor.moveToNext();
        }
        cursor.close();
        Gallery superItemGalleryView = (Gallery) findViewById(R.id.can_be_combined_to_gallery);
        GalleryItemImageAdapter sia = new GalleryItemImageAdapter(superItemGalleryView,this,ids,names,costs);
        superItemGalleryView.setAdapter(sia);
        sia.adjustSelection(); // bloody damned gallery!
    }
    
    private void setShops ( Cursor cursor ) {
        int[] ids = {
                cursor.getInt(cursor.getColumnIndex(ColumnNames.Items.shop1_image_id)),
                cursor.getInt(cursor.getColumnIndex(ColumnNames.Items.shop2_image_id)),
                cursor.getInt(cursor.getColumnIndex(ColumnNames.Items.shop3_image_id)),
        };
        String[] names = {
                cursor.getString(cursor.getColumnIndex(ColumnNames.Items.shop1)),
                cursor.getString(cursor.getColumnIndex(ColumnNames.Items.shop2)),
                cursor.getString(cursor.getColumnIndex(ColumnNames.Items.shop3)),
        };
        Gallery shopGalleryView = (Gallery) findViewById(R.id.shops_gallery);
        GalleryShopImageAdapter sia = new GalleryShopImageAdapter(shopGalleryView,this,ids,names);
        shopGalleryView.setAdapter(sia);
        sia.adjustSelection(); // bloody damned gallery!
    }
    
    private void addBooleanIfHasValue ( Cursor cursor, TableLayout tableLayout, String colName, int labelId ) {
        int colIndex = cursor.getColumnIndex(colName); 
        if ( cursor.isNull(colIndex) )
            return;
        int i = cursor.getInt(colIndex);
        String val = i == 0 ? getString(R.string.no) : getString(R.string.yes);
        addStrings(tableLayout,labelId,val,-1);
    }

    private void addIntIfHasValue ( Cursor cursor, TableLayout tableLayout, String colName, int labelId, int label2Id ) {
        int colIndex = cursor.getColumnIndex(colName); 
        if ( cursor.isNull(colIndex) )
            return;
        String val = "" + cursor.getInt(colIndex);
        addStrings(tableLayout,labelId,val,label2Id);
    }

    private void addStringIfHasValue ( Cursor cursor, TableLayout tableLayout, String colName, int labelId ) {
        String val = cursor.getString(cursor.getColumnIndex(colName));
        if ( val == null || val.length() < 1 )
            return;
        addStrings(tableLayout,labelId,val,-1);
    }

    private void addStringIfHasValue ( Cursor cursor, TableLayout tableLayout, String colName, int labelId, int labelId2 ) {
        String val = cursor.getString(cursor.getColumnIndex(colName));
        if ( val == null || val.length() < 1 )
            return;
        addStrings(tableLayout,labelId,val,labelId2);
    }

    private void addStrings ( TableLayout tableLayout, int labelId, String val, int label2Id ) {
        String labelTxt = getString(labelId);
        TableRow tr = new TableRow(this);
        tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT,
                                                     LayoutParams.WRAP_CONTENT));
        TextView label = new TextView(this);
        label.setText(labelTxt);
        tr.addView(label);
        TextView txt = new TextView(this);
        txt.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT,
                                                      LayoutParams.WRAP_CONTENT));
        txt.setGravity(0x05); // TODO: find constant "right" wherever it is!
        txt.setSingleLine(false);
        txt.setHorizontallyScrolling(false);
        txt.setEllipsize(TruncateAt.END);
        txt.setTypeface(Typeface.DEFAULT_BOLD);
        txt.setText(val);
        tr.addView(txt);
        if ( label2Id != -1 ) {
            labelTxt = getString(label2Id);
            label = new TextView(this);
            label.setPadding(5,0,0,0);
            label.setText(labelTxt);
            tr.addView(label);
        }
        tableLayout.addView(tr);
    }

}
